/**
 * Created by yihua on 2017/3/7.
 */
$(document).ready(function () {
    $('#addcategory').on('shown.bs.modal', function (e) {
        $("#addForm").find("input").val('');
    })
    $("#toolbar-add").click(function () {
        $("#addcategory").modal("show");
        $("#add_confirm").unbind('click').click(function () {
            //$("#deleteModal").modal("hide");
            var options = {
                url: '/circle/addcirclecate',
                beforeSubmit: validate,  // pre-submit callback
                success: showResponse,  // post-submit callback
                dataType: 'json',
                type: 'get',
                data: {}
            };
            $("#addForm").ajaxSubmit(options);
        });
        function validate(formData) {
            for (var i = 0; i < formData.length; i++) {
                if ('file' == formData[i].name) {
                    break;
                }
                if (!formData[i].value) {
                    console.log(formData[i].name)
                    toastr.error('请输入所有选项', '', {positionClass: 'toast-bottom-center'});
                    return false;
                }
            }
        }

        function showResponse(responseText) {

            if (0 !== responseText.errno) {
                toastr.error(responseText.errmsg, '', {positionClass: 'toast-bottom-center'});
            }
            else {
                toastr.success('增加成功', '', {positionClass: 'toast-bottom-center'});
                $('#addcategory').modal('hide');
                window.location.reload();
            }
        }
    });

});